//
//  SAInventoryDetailCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/11/4.
//

#import "SAInventoryDetailCell.h"
#import "SAInventoryDetailModel.h"
#import "SAInventoryWarningModel.h"

@interface SAInventoryDetailCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *colourLab;
@property (nonatomic, strong) UILabel *sizeLab;
//@property (nonatomic, strong) UILabel *inventoryLab;
//@property (nonatomic, strong) UILabel *ceilingLab;
//@property (nonatomic, strong) UILabel *limitLab;

@property (nonatomic, strong) UITextField *inventoryTf;
@property (nonatomic, strong) UITextField *ceilingTf;
@property (nonatomic, strong) UITextField *limitTf;


@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation SAInventoryDetailCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAInventoryDetailList *)model {
    _model = model;
    self.colourLab.text = model.colourName;
    self.sizeLab.text = model.sizeName;
    self.inventoryTf.text = model.stock;
    if ([model.stockUp isEqualToString:@"null"]) {
        self.ceilingTf.text = @"";
    }else {
        self.ceilingTf.text = model.stockUp;
    }
    if ([model.stockDown isEqualToString:@"null"]) {
        self.limitTf.text = @"";
    }else {
        self.limitTf.text = model.stockDown;
    }
    
    if (self.isEdit == YES) {
        self.inventoryTf.userInteractionEnabled = YES;
        self.ceilingTf.userInteractionEnabled = YES;
        self.limitTf.userInteractionEnabled = YES;
        self.inventoryTf.layer.borderColor = kBuleColor.CGColor;
        self.ceilingTf.layer.borderColor = kBuleColor.CGColor;
        self.limitTf.layer.borderColor = kBuleColor.CGColor;
    }else {
        self.inventoryTf.userInteractionEnabled = NO;
        self.ceilingTf.userInteractionEnabled = NO;
        self.limitTf.userInteractionEnabled = NO;
        self.inventoryTf.layer.borderColor = [UIColor whiteColor].CGColor;
        self.ceilingTf.layer.borderColor = [UIColor whiteColor].CGColor;
        self.limitTf.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    
    _colourLab.frame = SARectMake(20, 0, 55, 34);
    _sizeLab.frame = SARectMake(80, 0, 65, 34);
    _inventoryTf.frame = SARectMake(155, 4, 50, 26);
    _ceilingTf.frame = SARectMake(210, 4, 75, 26);
    _limitTf.frame = SARectMake(290, 4, 75, 26);
}

-(void)setWarnModel:(SAInventoryWarningList *)warnModel {
    _warnModel = warnModel;
    self.imageV.hidden = YES;
    self.colourLab.text = warnModel.colourName;
    self.sizeLab.text = warnModel.sizeName;
    self.inventoryTf.text = warnModel.stock;
    if ([warnModel.stockUp isEqualToString:@"null"]) {
        self.ceilingTf.text = @"--";
    }else {
        self.ceilingTf.text = warnModel.stockUp;
    }
    if ([warnModel.stockDown isEqualToString:@"null"]) {
        self.limitTf.text = @"--";
    }else {
        self.limitTf.text = warnModel.stockDown;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 6){
        return NO;
    }
    if (self.inventoryTf == textField && self.inventoryTf.text.length == 0 && [string isEqualToString:@"-"]) {
        return YES;
    }
    return [self validateNumber:string];
}

- (void)textFieldEditChanged:(UITextField *)textField {
    NSString *newStr = [NSString stringWithFormat:@"%zd",[textField.text integerValue]];
    if (textField.text.length == 0) {
        newStr = @"";
    }
    if (self.inventoryTf == textField) {
        if ([textField.text isEqualToString:@"-"]) {
            
        }else {
            textField.text = newStr;
        }
        self.model.stock = newStr;
    }else if (self.ceilingTf == textField) {
        textField.text = newStr;
        self.model.stockUp = newStr;
    }else if (self.limitTf == textField) {
        textField.text = newStr;
        self.model.stockDown = newStr;
    }
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - initUI
-(void)initUI {
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.colourLab];
    [self.contentView addSubview:self.sizeLab];
    [self.contentView addSubview:self.inventoryTf];
    [self.contentView addSubview:self.ceilingTf];
    [self.contentView addSubview:self.limitTf];
    [self.contentView addSubview:self.imageV];
}

#pragma mark - lazy
-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 34, kBaseScreenWidth, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:SARectMake(kBaseScreenWidth - 30, 10, 8, 15)];
        _imageV.image = [UIImage imageNamed:@"inventory_rightRedAllow"];
    }
    return _imageV;
}

-(UILabel *)colourLab {
    if (!_colourLab) {
        _colourLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 60, 34)];
        _colourLab.textColor = kBlackColor;
        _colourLab.textAlignment = NSTextAlignmentLeft;
        _colourLab.numberOfLines = 0;
        _colourLab.font = kFont(13);
    }
    return _colourLab;
}

-(UILabel *)sizeLab {
    if (!_sizeLab) {
        _sizeLab = [[UILabel alloc] initWithFrame:SARectMake(80, 0, 60, 34)];
        _sizeLab.textColor = kBlackColor;
        _sizeLab.textAlignment = NSTextAlignmentCenter;
        _sizeLab.numberOfLines = 0;
        _sizeLab.font = kFont(13);
    }
    return _sizeLab;
}

//-(UILabel *)inventoryLab {
//    if (!_inventoryLab) {
//        _inventoryLab = [[UILabel alloc] initWithFrame:SARectMake(140, 0, 60, 34)];
//        _inventoryLab.textColor = kBlackColor;
//        _inventoryLab.textAlignment = NSTextAlignmentCenter;
//        _inventoryLab.numberOfLines = 0;
//        _inventoryLab.font = kFont(13);
//    }
//    return _inventoryLab;
//}
//
//-(UILabel *)ceilingLab {
//    if (!_ceilingLab) {
//        _ceilingLab = [[UILabel alloc] initWithFrame:SARectMake(200, 0, 70, 34)];
//        _ceilingLab.textColor = kBlackColor;
//        _ceilingLab.textAlignment = NSTextAlignmentCenter;
//        _ceilingLab.numberOfLines = 0;
//        _ceilingLab.font = kFont(13);
//    }
//    return _ceilingLab;
//}
//
//-(UILabel *)limitLab {
//    if (!_limitLab) {
//        _limitLab = [[UILabel alloc] initWithFrame:SARectMake(270, 0, 70, 34)];
//        _limitLab.textColor = kBlackColor;
//        _limitLab.textAlignment = NSTextAlignmentCenter;
//        _limitLab.numberOfLines = 0;
//        _limitLab.font = kFont(13);
//    }
//    return _limitLab;
//}

-(UITextField *)inventoryTf {
    if (!_inventoryTf) {
        _inventoryTf = [[UITextField alloc] initWithFrame:SARectMake(145, 4, 50, 26)];
        _inventoryTf.textColor = kBlackColor;
        _inventoryTf.textAlignment = NSTextAlignmentCenter;
        _inventoryTf.font = kFont(13);
        _inventoryTf.userInteractionEnabled = NO;
        _inventoryTf.layer.borderColor = [UIColor whiteColor].CGColor;
        _inventoryTf.layer.cornerRadius = kScaleNum(3);
        _inventoryTf.layer.borderWidth = kScaleNum(1);
        _inventoryTf.layer.masksToBounds = YES;
        _inventoryTf.delegate = self;
        _inventoryTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        [_inventoryTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _inventoryTf;
}
-(UITextField *)ceilingTf {
    if (!_ceilingTf) {
        _ceilingTf = [[UITextField alloc] initWithFrame:SARectMake(205, 4, 60, 26)];
        _ceilingTf.textColor = kBlackColor;
        _ceilingTf.textAlignment = NSTextAlignmentCenter;
        _ceilingTf.font = kFont(13);
        _ceilingTf.userInteractionEnabled = NO;
        _ceilingTf.layer.borderColor = [UIColor whiteColor].CGColor;
        _ceilingTf.layer.cornerRadius = kScaleNum(3);
        _ceilingTf.layer.borderWidth = kScaleNum(1);
        _ceilingTf.layer.masksToBounds = YES;
        _ceilingTf.delegate = self;
        _ceilingTf.keyboardType = UIKeyboardTypeNumberPad;
        [_ceilingTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _ceilingTf;
}

-(UITextField *)limitTf {
    if (!_limitTf) {
        _limitTf = [[UITextField alloc] initWithFrame:SARectMake(275, 4, 60, 26)];
        _limitTf.textColor = kBlackColor;
        _limitTf.textAlignment = NSTextAlignmentCenter;
        _limitTf.font = kFont(13);
        _limitTf.userInteractionEnabled = NO;
        _limitTf.layer.borderColor = [UIColor whiteColor].CGColor;
        _limitTf.layer.cornerRadius = kScaleNum(3);
        _limitTf.layer.borderWidth = kScaleNum(1);
        _limitTf.layer.masksToBounds = YES;
        _limitTf.delegate = self;
        _limitTf.keyboardType = UIKeyboardTypeNumberPad;
        [_limitTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _limitTf;
}

@end
